package com.redare.dev.smart.generator.workbench.controller;

import com.redare.dev.smart.generator.workbench.config.WebConfig;
import com.redare.devframework.common.pojo.CommonResult;
import com.redare.devframework.webplatform.sdk.core.PlatformSdk;
import com.redare.devframework.webplatform.sdk.core.UserSdk;
import com.redare.devframework.webplatform.sdk.core.arg.AccountArg;
import com.redare.devframework.webplatform.sdk.core.arg.PlatformArg;
import com.redare.devframework.webplatform.sdk.core.pojo.User;
import com.redare.devframework.webplatform.workbench.controller.component.WorkbenchController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @author hxw
 */
@Controller
public class WebWorkbenchController extends WorkbenchController {

    @Autowired
    PlatformSdk platformSdk;

    @Autowired
    UserSdk userSdk;

    @Autowired
    WebConfig config;


    @ResponseBody
    @RequestMapping(value = "/api/login", method = RequestMethod.POST)
    public CommonResult<User> login(
            @RequestParam(value = "account", required = false) String account,
            @RequestParam(value = "pwd", required = false) String pwd,
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        CommonResult<User> result = platformSdk.loginPlatform(
                new PlatformArg.Login()
                        .setAccount(account)
                        .setPwd(pwd)
        );
        if (result.isNotSuccess()) {
            return result;
        }
        saveLoginUserInfo(request, result.getData());
        return returnSuccessWrapper();
    }

    @ResponseBody
    @RequestMapping(value = "/api/loginOut", method = RequestMethod.POST)
    public CommonResult<Void> loginOut(
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        removeLoginUserInfo(request);
        return returnSuccessWrapper();
    }


    @ResponseBody
    @RequestMapping(value = "/api/getLoginUser", method = RequestMethod.POST)
    public CommonResult<User> getLoginUser(
            HttpServletRequest request
    ) {
        User user = getLoginUserInfo(request);
        User data = userSdk.getUser(new AccountArg.Get().setAccount(user.getAccount())).getData();
        return returnSuccessWrapper(data);
    }

    @Override
    protected Map<String, Object> initWorkbenchIndexData(HttpServletRequest request) {
        Map<String, Object> arg = new HashMap<>();
        arg.put("WEB_ROOT_URL", config.getWebRootUrl());
        arg.put("DING_LOGIN_APP", config.getDingLoginApp());
        arg.put("RES_FILE_URL", config.getResourceFileUrl());
        arg.put("RES_IMAGE_URL", config.getResourceImageUrl());
        arg.put("RES_URL", config.getResourceUrl());
        arg.put("CTX_RES", config.getResourceUrl());
        return arg;
    }

}
